package com.github.szhittech.service;


import com.github.szhittech.bean.LibraryBean;
import com.github.szhittech.bean.VersionBean;
import com.github.szhittech.bean.ext.ClifeArgsExt;
import com.github.szhittech.service.api.inter.IDependency;
import com.github.szhittech.service.prop.Properties;
import com.github.szhittech.util.Util;
import com.github.szhittech.util.WordUtil;

import org.gradle.api.artifacts.Dependency;

import java.util.List;

public class DependencyImpl implements IDependency<ClifeArgsExt> {
    @Override
    public String onDependency(ClifeArgsExt args, Dependency dependency) {
        if (Properties.SERVER_LIBS == null)
            return null;
        List<LibraryBean> libs = Properties.SERVER_LIBS.getLibs();
        if (libs == null || libs.size() <= 0)
            return null;
        if (dependency == null)
            return null;
        String groupName = dependency.getGroup() + ":" + dependency.getName();
        LibraryBean dest = Properties.SERVER_LIBS_MAP.get(groupName);
        if (dest == null)
            return null;
        VersionBean destVersion = dest.getLastVersion();
        if (destVersion == null)
            return null;

        String destVersionName = destVersion.getVersionName();
        String localVersionName = dependency.getVersion();
        if (destVersionName == null || localVersionName == null)
            return null;
        String localFullName = dependency.getGroup() + ":" + dependency.getName() + ":" + dependency.getVersion();
        int cc = Util.versionCompareTo(destVersionName, localVersionName);


        if ((args != null && args.isIgnVersionWarm()) && destVersion.getForceUpdate() == 0)
            return null;

        if (cc <= 0)
            return null;
        String warm = "";
        if (destVersion.getForceUpdate() == 1) {
            warm = WordUtil.STRING_11;
        }

        StringBuffer sBuffer = new StringBuffer();
        sBuffer.append(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
        sBuffer.append("\r\n");
        sBuffer.append(WordUtil.STRING_12);
        sBuffer.append(localFullName);
        sBuffer.append("\r\n");
        sBuffer.append(WordUtil.STRING_13);
        sBuffer.append(dest.getFullyQualifiedName());
        sBuffer.append("\r\n");
        sBuffer.append(WordUtil.STRING_14);
        sBuffer.append(dest.toDescribetion());
        sBuffer.append("\r\n");
        if (!warm.equalsIgnoreCase("")) {
            sBuffer.append(warm);
            sBuffer.append("\r\n");
        }
        sBuffer.append("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>");
        sBuffer.append("\r\n");
        if (destVersion.getForceUpdate() == 1) {
            throw new IllegalArgumentException(sBuffer.toString());
        }

        System.err.println(sBuffer.toString());
        return sBuffer.toString();
    }
}
